package com.nb.hdj.util;

import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: yaml工具类
 * @Author: hedongjia
 * @Date: 2021/11/5 下午4:35
 * @Version: v1.0
 */
public class YamlReadUtils {
    private static Map<String, Map<String, Object>> properties;
    /**
     * 获取yaml属性
     *
     * @param key
     * @return
     */
    InputStream in = null;
    public Object getValueByKey(String root, String key) {
        try {
            properties = new HashMap<>();
            Yaml yaml = new Yaml();
            //System.out.println(YamlReadUtils.class.getResource(""));
            //System.out.println(YamlReadUtils.class.getResource("../data/login1.yaml"));
            in = YamlReadUtils.class.getResourceAsStream("../data/");
            properties = yaml.loadAs(in, HashMap.class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String, Object> rootProperty = properties.get(root);
        return rootProperty.getOrDefault(key, "");
    }

    /**
     * 获取String yaml属性
     *
     * @param key
     * @return
     */
    public String getStringValueByKey(String root, String key, String yamlName) {
        try {
            properties = new HashMap<>();
            Yaml yaml = new Yaml();
            //System.out.println(YamlReadUtils.class.getResource(""));
            //System.out.println(YamlReadUtils.class.getResource("../data/login1.yaml"));
            in = YamlReadUtils.class.getResourceAsStream("../data/"+yamlName);
            properties = yaml.loadAs(in, HashMap.class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String, Object> rootProperty = properties.get(root);
        if(rootProperty!=null){
            return String.valueOf(rootProperty.getOrDefault(key, ""));
        }
       return "获取到的yaml的root值为空";
    }

    /**
     * 获取Map yaml 属性
     * @param root,yamlName
     * @return
     */
    public Map getMapValueByKey(String root,String yamlName) {
        try {
            properties = new HashMap<>();
            Yaml yaml = new Yaml();
            in = YamlReadUtils.class.getResourceAsStream("../data/"+yamlName);
            properties = yaml.loadAs(in, HashMap.class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String, Object> rootProperty = properties.get(root);
        Map<String,String> ObjToString=new HashMap<>();
        if(rootProperty!=null){
            /**
             * 通过map.entrySet遍历key和value
             */
            for(Map.Entry<String, Object> entry:rootProperty.entrySet()){
                ObjToString.put(entry.getKey(),String.valueOf(entry.getValue()));
                }
            return ObjToString;
        }
        return null;
    }
}
